package com.zhisuo.apicenter.service.imp;


import com.zhisuo.apicenter.model.*;
import com.zhisuo.apicenter.service.ApiService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ApiServiceImp extends BaseServiceImp implements ApiService {


    @Override
    public ZhiSuoUser getUserIdByOpenId(String openid) {
        String sql = "select * from zhisuo_user where openid = ? and status =1 limit 1";
        return getEntry(sql, new ZhiSuoUserRowMapper(), new Object[]{openid});
    }

    @Override
    public String insertNewUserByOpenId(String openid) {
        String sql = "insert into zhisuo_user(openid,create_time,status) values(?,?,?)";
        return saveAndGetStringId(sql, new Object[]{openid,new Date(),1});
    }

    @Override
    public String getMobileSecret(String mobile) {
        String sql = "select id from zhisuo_user_secret where mobile = ? limit 1";
        return queryString(sql, new Object[]{mobile});
    }

    @Override
    public void insertMobileSecret(String mobile, String secret) {
        String sql = "insert into zhisuo_user_secret(mobile,secret,create_time,end_time) values(?,?,?,?)";
        long curren = System.currentTimeMillis();
        curren += 10 * 60 * 1000;
        Date da = new Date(curren);
        update(sql, new Object[]{mobile,secret,new Date(),da});
    }

    @Override
    public void updateMObileSecret(String mobile, String secret, String id) {
        String sql="update zhisuo_user_secret set mobile=?,secret=?,end_time=?,create_time=? where id=?";
        long curren = System.currentTimeMillis();
        curren += 10 * 60 * 1000;
        Date da = new Date(curren);
        update(sql, new Object[]{mobile,secret,da,new Date(),id});
    }

    @Override
    public List<ParkArea> getListParkArea() {
        String sql="select * from parkarea_info where status=1";
        return getEntryList(sql,new ParkAreaRowMapper(),new Object[]{});
    }

    @Override
    public List<ParkAreaBuild> getListParkAreaBuildByParkAreaId(String parkareaid) {
        String sql="select * from parkarea_build_info where parkareaid=? and status=1";
        return getEntryList(sql,new ParkAreaBuildRowMapper(),new Object[]{parkareaid});
    }

    @Override
    public String getUserSecretIdByMobileAndSecret(String mobile, String secret) {
        String sql="select id from zhisuo_user_secret where mobile=? and secret=?";
        return queryString(sql,new Object[]{mobile,secret});
    }

    @Override
    public void updateMobileByUserId(String mobile, String userid, int role, String salesmanid) {
        String sql="update zhisuo_user set mobile=?,role=?, salemansid=? where id=?";
        update(sql,new Object[]{mobile,role,salesmanid,userid});
    }

    @Override
    public String getUserAddressIdByUserIdAndPrkareabuildid(String userid, String parkareabuildid) {
        String sql="select id from zhisuo_user_address where userid=? and parkareabuildid=?";
        return queryString(sql,new Object[]{userid,parkareabuildid});
    }

    @Override
    public void insertNewUserAddrss(String userid, String parkareabuildid,String flag) {
        String sql = "insert into zhisuo_user_address(userid,parkareabuildid,status,flag) values(?,?,?,?)";
        update(sql, new Object[]{userid,parkareabuildid,1,flag});
    }

    @Override
    public String getSalesManIdByMobile(String mobile) {
        String sql="select id from zhisuo_user_salesman where mobile=? and status=?";
        return queryString(sql,new Object[]{mobile,1});
    }

    @Override
    public Integer getUserRoleByUserId(String userid) {
        String sql="select role from zhisuo_user where id=?";
        return queryInteger(sql,new Object[]{userid});
    }

    @Override
    public ZhiSuoUser getUserInfoByUserId(String userid) {
        String sql = "select * from zhisuo_user where id = ? and status =1 limit 1";
        return getEntry(sql, new ZhiSuoUserRowMapper(), new Object[]{userid});
    }

    @Override
    public Integer getUserIdByMobile(String receiptusermobile) {
        String sql="select id from zhisuo_user where mobile=?";
        return queryInteger(sql,new Object[]{receiptusermobile});
    }

    @Override
    public ZhiSuoUser getUserRoleByMobile(String mobile) {
        String sql = "select * from zhisuo_user where mobile = ? and status =1 limit 1";
        return getEntry(sql, new ZhiSuoUserRowMapper(), new Object[]{mobile});
    }

    @Override
    public void updateUserInfoById(String userid, String avatarurl, String nickname, String city, String province, String country, String gender, String birthday) {
        String sql="update zhisuo_user set avatarurl=?,nickname=?,gender=?,province=?,city=?,country=?,birthday=? where id=?";
        update(sql,new Object[]{avatarurl,nickname,gender,province,city,country,birthday,userid});
    }

    @Override
    public void updateUserAddressIdByUserIdAndPrkareabuildid(String addressid, String parkareabuildid, String flag) {
        String sql="update zhisuo_user_address set parkareabuildid=?,flag=? where id=?";
        update(sql,new Object[]{parkareabuildid,flag,addressid});
    }

    @Override
    public void deleteUserAddressById(String addressid) {
        String sql="delete from  zhisuo_user_address  where id=?";
        update(sql,new Object[]{addressid});
    }

    @Override
    public String getSellerManIdByMobile(String mobile) {

        String sql="select id from zhisuo_user_sellersman where mobile=? and status=?";
        return queryString(sql,new Object[]{mobile,1});
    }

    @Override
    public void updateUserAddressFlag(String userid) {
        String sql="update zhisuo_user_address set flag=0 where userid=?";
        update(sql,new Object[]{userid});

    }

    @Override
    public void updateUserAddressFlagStatus(String useraddressid, String flag) {
        String sql="update zhisuo_user_address set flag=? where id=?";
        update(sql,new Object[]{flag,useraddressid});
    }

    @Override
    public Integer getAwardIntegeralByConfig(String award_integral) {
        String sql="select val from config where code=?";
        return queryInteger(sql,new Object[]{award_integral});
    }

    @Override
    public String getZhiSuoUserSignRecordByUserDate(String userid, String daydate) {
        String sql="select id from zhisuo_user_signrecord where userid=? and daydate=?";
        return queryString(sql,new Object[]{userid,daydate});
    }

    @Override
    public List<String> getListZhiSuoUserSignRecordByMonth(String userid) {
        String sql = "select * from zhisuo_user_signrecord where userid = ? and date_format(create_time,'%Y-%m')=date_format(now(),'%Y-%m')";
        return queryStringArray(sql, new Object[]{userid});
    }

    @Override
    public List<ZhiSuoUserIntegralRecord> getListZhiSuoUserIntegralRecordByUserId(String userid, Integer page, Integer pagesize) {
        String sql= "select * from zhisuo_user_integral_record where userid=? order by createtime desc limit ?,?";
        return getEntryList(sql,new ZhiSuoUserIntegralRecordRowMapper(),new Object[]{userid, (page - 1) * pagesize, pagesize});
    }

    @Override
    public List<String> getListPhoneByPhoneNumber(String phonenumber) {
        String sql="select mobile from zhisuo_user where mobile like ?";
        return queryStringArray(sql,new Object[]{"%"+phonenumber+"%"});
    }

    @Override
    public String getTcpUrlByTcpCenterUrl(String tcp_center_url) {
        String sql="select val from config where code=?";
        return queryString(sql,new Object[]{tcp_center_url});
    }

    @Override
    public List<String> getListSysIndexImgByStatus() {
        String sql="select image_url from sys_indeximg where status=1";
        return queryStringArray(sql,new Object[]{});
    }
}
